package com.example.hospitalManage.controller.admin.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hospitalManage.entity.Schedule;
import com.example.hospitalManage.service.ScheduleService;
import com.example.hospitalManage.utils.Response;
import com.example.hospitalManage.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Description:
 * Param:
 * return:
 * Author: 彭雨胜
 * Data: 2022/12/30
 */
@RestController
@RequestMapping("/admin")
@Api(value = "v1",tags = "医院排班相关接口")
@CrossOrigin
public class ScheduleController {
    @Autowired
    private ScheduleService scheduleService;

    //定义日志对象
    public final static Logger log = LoggerFactory.getLogger(OrderOrderInfoController.class);

    // 获取所有排班信息
    @ApiOperation(value = "获取所有排班信息")
    @GetMapping("schedule")
    public ResponseResult<List<Schedule>> findAllSchedule() {
        List<Schedule> scheduleList = scheduleService.list();
        return Response.createOkResp(scheduleList);
    }

    //根据医院编号查询排班
    @ApiOperation(value ="根据医院编号查询排班")
    @PostMapping("schedule/{currentPage}/{limit}")
    public ResponseResult getScheduleRule(@PathVariable long currentPage,
                                  @PathVariable long limit,
                                  @RequestBody Schedule schedule) {
        Page<Schedule> page = new Page<>(currentPage, limit);

        QueryWrapper<Schedule> wrapper = new QueryWrapper<>();
        String hosCode = schedule.getHoscode();
        Date wordDate = schedule.getWorkDate();
        String depCode =  schedule.getDepcode();

        if(!StringUtils.isEmpty(hosCode)) {
            wrapper.eq("hoscode", hosCode);
        }

        if(!StringUtils.isEmpty(wordDate)) {
            wrapper.eq("work_date", wordDate);
        }

        if(!StringUtils.isEmpty(depCode)) {
            wrapper.eq("depcode", depCode);
        }

        //调用方法实现分页查询
        IPage<Schedule> pageSchedule = scheduleService.page(page, wrapper);

        //返回结果
        return Response.createOkResp(pageSchedule);
    }
}
